

package yf.mall.service.impl.app;

import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.enums.app.AppCategoryTypeEnum;
import yf.mall.vo.up.AppCategoryUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.vo.down.AppCategoryDown;

import java.util.Arrays;
import java.util.List;
import java.net.URLEncoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.AppCategoryDao;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.vo.AppCategoryVo;
import yf.mall.service.app.AppCategoryService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * 
 *
 * @date Tue Jul 18 14:30:26 CST 2023
 */
@Service
public class AppCategoryServiceImpl extends BaseServiceImpl<AppCategoryEntity> implements AppCategoryService {

    @Resource
    private AppCategoryDao dao;


    @Autowired
    public void setDao(AppCategoryDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppCategoryVo> findVo(AppCategoryVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppCategoryVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppCategoryVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppCategoryVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppCategoryVo> list) {
        try {
            List<AppCategoryDown> datas = list.stream().map(item -> new AppCategoryDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppCategoryDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppCategoryUp.class, new UploadDataListener()).sheet().doRead();
    }

    @Override
    public AppCategoryEntity getRootCategory(Long type) {
        if (type == null || type <= 0) {
            return null;
        }
        AppCategoryEntity example = new AppCategoryEntity();
        example.setType(type);
        example.setLevel(0);
        example.setParentType(0L);
        example.setRootId(0L);
        List<AppCategoryEntity> result = dao.find(example);
        return result.stream().findFirst().orElse(null);
    }

    @Override
    public List<AppCategoryEntity> getCategoryByParent(Long parentType, Long rootId) {
        if (parentType == null || parentType <= 0L || rootId == null) {
            return null;
        }
        AppCategoryEntity example = new AppCategoryEntity();
        example.setParentType(parentType);
        example.setRootId(rootId);
        return dao.find(example);
    }

    class UploadDataListener extends AnalysisEventListener<AppCategoryUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppCategoryUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
    public List<AppCategoryEntity> findByLevel(Integer level) {
        return dao.findByLevel(level);
    }

    @Override
    public List<AppCategoryEntity> findForMaterial() {
        //原始素材
        AppCategoryEntity ORIGINAL_MATERIAL = dao.findByType(AppCategoryTypeEnum.ORIGINAL_MATERIAL_ROOT.getType());
        ORIGINAL_MATERIAL.setChildren(dao.findByParent(ORIGINAL_MATERIAL.getType()));
        //图文素材
        AppCategoryEntity GRAPHIC = dao.findByType(AppCategoryTypeEnum.GRAPHIC_TYPE_ROOT.getType());
        GRAPHIC.setChildren(dao.findByParent(GRAPHIC.getType()));
        return Arrays.asList(ORIGINAL_MATERIAL, GRAPHIC);
    }

    @Override
    public List<AppCategoryEntity> findForProductMaterialType() {
        List<AppCategoryEntity> items =  dao.findByParent(AppCategoryTypeEnum.MATERIAL_TAB_ROOT.getType());
        return items;
    }

    @Override
    public List<AppCategoryEntity> findForCourse() {
        return dao.findByParent(AppCategoryTypeEnum.COURSE_ROOT.getType());
    }

    @Override
    public List<AppCategoryEntity> findForProduct() {
        //APP
        AppCategoryEntity APP_TYPE = dao.findByType(AppCategoryTypeEnum.PRODUCT_MARKET_ROOT.getType());
        APP_TYPE.setChildren(dao.findByParent(APP_TYPE.getType()));
        //小程序
        AppCategoryEntity WX_TYPE = dao.findByType(AppCategoryTypeEnum.PRODUCT_CATEGORY_ROOT.getType());
        WX_TYPE.setChildren(dao.findByParent(WX_TYPE.getType()));
        return Arrays.asList(APP_TYPE, WX_TYPE);
    }

    @Override
    public List<AppCategoryEntity> findForAppProduct() {
        AppCategoryEntity APP_TYPE = dao.findByType(AppCategoryTypeEnum.PRODUCT_MARKET_ROOT.getType());
        return dao.findByParent(APP_TYPE.getType());
    }

    @Override
    public List<AppCategoryEntity> findForWxProduct() {
        AppCategoryEntity WX_TYPE = dao.findByType(AppCategoryTypeEnum.PRODUCT_CATEGORY_ROOT.getType());
        return dao.findByParent(WX_TYPE.getType());
    }
}
